package org.newlifecac;

import java.io.File;
import java.util.Properties;

import org.newlifecac.util.NetworkDetector;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("SdCardPath")
public class FilePickerActivity extends Activity
{

	private ListView listViewFilePicker;
	private File mCurrentDirectory;
	FilePickerAdapter mFileAdapter;
	private ImageView iv_cancel;
	private ImageView iv_ok;
	private TextView tv_path;
	
	
	private static String SD_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
	public static File PIC_PATH = new File(SD_PATH + "/Picture/");
	public static File CONFIG_PATH = new File(SD_PATH + "/Newlifecac/");
	String fileEndings[] ={ "jpgee" };
	String imagePath = "";

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filelist);
		listViewFilePicker = (ListView) findViewById(R.id.filelist);
		listViewFilePicker.setCacheColorHint(0x00000000);
		mFileAdapter = new FilePickerAdapter(this);
		listViewFilePicker.setAdapter(mFileAdapter);
		//iv_cancel = (ImageView) findViewById(R.id.iv_cancel);
		//iv_cancel.setOnClickListener(onClickListener);

		iv_ok = (ImageView) findViewById(R.id.iv_ok);
		iv_ok.setOnClickListener(onClickListener);
		
		tv_path = (TextView) findViewById(R.id.tv_path);
		mCurrentDirectory = new File(Environment
				.getExternalStorageDirectory().getAbsolutePath());

		ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener()
		{

			public void onItemClick(AdapterView parent, View view,
					int position, long id)
			{
				if (position==0)
				{
					Intent i0 = new Intent();
					i0.setClass(FilePickerActivity.this, InfoShow.class);
					FilePickerActivity.this.startActivityForResult(i0,5);
				}
				if (position==1)
				{
					check();
					Toast("Don't Finish! Come Soon!");
					/*Intent i1 = new Intent();
					i1.setClass(FilePickerActivity.this, GetHtml.class);
					FilePickerActivity.this.startActivityForResult(i1,CONTEXT_RESTRICTED);*/
				}
				if (position==2)
				{
					Intent i2 = new Intent();
					i2.setClass(FilePickerActivity.this, SMSSender.class);
					FilePickerActivity.this.startActivityForResult(i2,CONTEXT_IGNORE_SECURITY);
				}
				if (position==3)
				{
					check();
					Intent i3 = new Intent();
					i3.setClass(FilePickerActivity.this, ToolsActivity.class);
					FilePickerActivity.this.startActivityForResult(i3,6);
				}
				if (position==4)
				{
					check();
					updateInfo("Update Notice","the Newest Information?");
				}
				
			}
		};
		listItems(mCurrentDirectory);
		listViewFilePicker.setOnItemClickListener(lv2click);
		
	}
	
	public void updateInfo(String title, String message)
	{

		Builder dialog = new AlertDialog.Builder(FilePickerActivity.this);
		dialog.setTitle(title).setIcon(android.R.drawable.ic_dialog_info)
				.setMessage(message)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int which)
					{
						download();
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int which)
					{
						dialog.cancel();
					}
				}).create().show();
	
	}
	
	public void check()
	{
		if(!android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState()))
		{
			Toast("No Valid SDCard!");
			return;
		}
			
		if (!PIC_PATH.exists())
		{
			PIC_PATH.mkdirs();
			Toast("Create a picture Folder");
		}
		
		if (!CONFIG_PATH.exists())
		{
			CONFIG_PATH.mkdir();
			Toast("Create a configuration Folder");
		}
		if (NetworkDetector.detect(this)==false)
		{
			new AlertDialog.Builder(this)
					.setTitle("Network Error")
					.setMessage("No Network Connnection")
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int which)
								{
									return;
//									android.os.Process.killProcess(android.os.Process.myPid());
//									System.exit(0);
								}
							}).show();
		}
		
	}
	
	public void download()
	{
		DownloadAndStore downloadAndStore = new DownloadAndStore();
		downloadAndStore.download("Picture","http://www.newlifecac.org/properties/config.properties");
		downloadAndStore.download("Newlifecac", "http://www.newlifecac.org/properties/message.properties");
		loadConfig();
	}
	
	public void loadConfig()
	{
		BackupSetting bSetting = new BackupSetting();
		System.out.println("PIC_PATH-->"+PIC_PATH);
		Properties pro = bSetting.loadConfig(this, PIC_PATH+"/config.properties");
		DownloadAndStore downloadAndStore = null;
		for (int i = 0; i < pro.size(); i++)
		{
			downloadAndStore = new DownloadAndStore();
			downloadAndStore.download("Picture", (String) pro.get("pic"+(i+1)));
		}
	} 

	private OnClickListener onClickListener = new OnClickListener()
	{

		public void onClick(View v)
		{

			switch (v.getId())
			{
		/*	case R.id.iv_cancel:
				Intent mIntent = new Intent();
				setResult(RESULT_OK, mIntent);
				FilePickerActivity.this.finish();
				break;*/
			case R.id.iv_ok:
				
				break;
			}
		}

	};
	
	

	private void listItems(File aDirectory)
	{
		mFileAdapter.clearItems();
		mFileAdapter.notifyDataSetChanged();
		listViewFilePicker.postInvalidate();
		FileData fd1 =  new FileData();
		fd1.name = "Schedule";
		fd1.type = 0;
		mFileAdapter.addItem(fd1);
		
		
		FileData fd2 =  new FileData();
		fd2.name = "Sermons";
		fd2.type = 0;
		mFileAdapter.addItem(fd2);
		
		FileData fd3 =  new FileData();
		fd3.name = "Application";
		fd3.type = 0;
		mFileAdapter.addItem(fd3);
		
		
		FileData fd4=  new FileData();
		fd4.name = "Gallery";
		fd4.type = 0;
		mFileAdapter.addItem(fd4);
		
		FileData fd5 =  new FileData();
		fd5.name = "Update";
		fd5.type = 0;
		mFileAdapter.addItem(fd5);
		
		mFileAdapter.notifyDataSetChanged();
		listViewFilePicker.postInvalidate();
	}

	

	// --begin--Toast--//
	public void Toast(String str)
	{
		Toast.makeText(FilePickerActivity.this, str, Toast.LENGTH_LONG).show();
	}
	// --end--Toast--//
}
